package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.CartDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;
import com.sun.org.apache.bcel.internal.generic.DUP;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    Connection conn=null;
    PreparedStatement pre=null;
    ResultSet rs=null;
    @Override
    public List<Cart> listCart(Cart cart) throws Exception {
        try {
            List<Cart> list = new ArrayList<>();
            conn = DBUtil.getConnection();
            List params = new ArrayList();
            StringBuilder sql = new StringBuilder();
            sql.append(" SELECT c.*,b.*,f.* ");
            sql.append(" FROM cart c LEFT JOIN food f ");
            sql.append(" ON c.`foodId`=f.`foodId` ");
            sql.append(" LEFT JOIN business b ");
            sql.append(" ON b.`businessId`=c.`businessId` ");
            sql.append(" where 1=1 ");
            if (cart.getFoodId()!= null) {
                sql.append(" and c.foodId=? ");
                params.add(cart.getFoodId());
            }
            if (cart.getBusinessId()!= null) {
                sql.append(" and c.businessId=? ");
                params.add(cart.getBusinessId());
            }
            if (cart.getUserId() != null) {
                sql.append(" and c.userId=? ");
                params.add(cart.getUserId());
            }
            pre = conn.prepareStatement(sql.toString());
            //设置参数
            for (int i = 0; i < params.size(); i++) {
                pre.setObject(i+1, params.get(i));
                //将pre预编译对象中的问号的内容按照 填入params的顺序填入
                //   sql.append(" and c.foodId=? ");
                //   params.add(cart.getFoodId());
            }
            rs = pre.executeQuery();



            //如果能找到这样的数据 （就是select ......）一大堆然后还能找到，找到的是结果集rs
            // 然后从rs中get出来并set，将最终set完毕的东西添加到list，在Test中检验并输出即可


            while (rs.next()) {
                Cart c = new Cart();
                c.setCartId(rs.getInt("carId"));
                c.setBusinessId(rs.getInt("businessId"));
                c.setFoodId(rs.getInt("foodId"));
                c.setQuantity(rs.getInt("quality"));
                c.setUserId(rs.getString("userId"));
                Food food = new Food();
                food.setBusinessId(rs.getInt("businessId"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("remarks"));
                c.setFood(food);
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setRemarks(rs.getString("remarks"));
                c.setBusiness(business);
                list.add(c);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs,pre);
        }

    }

    @Override
    public int saveCart(Cart cart) throws Exception {
        try {
            int result=0;
            conn=DBUtil.getConnection();
            String sql="INSERT INTO cart VALUES(NULL,?,?,?,1)";
            pre=conn.prepareStatement(sql);
            //设置参数
            pre.setInt(1,cart.getFoodId());
            pre.setInt(2,cart.getBusinessId());
            pre.setString(3,cart.getUserId());
            result=pre.executeUpdate();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(pre);
        }

    }

    @Override
    public int removeCart(Cart cart) throws Exception {
        try {
            int result=0;
            conn=DBUtil.getConnection();
            StringBuilder sql=new StringBuilder("delete from cart where 1=1");
            List params=new ArrayList();
            if (cart.getUserId()!=null){
                sql.append(" and userId=? ");
                params.add(cart.getUserId());
            }
            if(cart.getBusinessId()!=null){
                sql.append(" and businessId=? ");
                params.add(cart.getBusinessId());
            }
            if(cart.getFoodId()!=null){
                sql.append(" and foodID=? ");
                params.add(cart.getFoodId());
            }
            //打印sql
            System.out.println(sql.toString());
            pre=conn.prepareStatement(sql.toString());
            for (int i = 0; i <params.size() ; i++) {
                pre.setObject(i+1,params.get(i));
            }
            result=pre.executeUpdate();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(pre);
        }

    }

    @Override
    public int updateCart(Cart cart) throws Exception{
        int result = 0;
        String sql = "update cart set quality=? where foodId=? and businessId=? and userId=?";
        try {
            conn = DBUtil.getConnection();
            pre= conn.prepareStatement(sql);
            pre.setInt(1, cart.getQuantity());
            pre.setInt(2, cart.getFoodId());
            pre.setInt(3, cart.getBusinessId());
            pre.setString(4, cart.getUserId());
            result = pre.executeUpdate();
        }finally {
            DBUtil.close(pre);
        }
        return result;
    }
}

